package com.fowo.api.flow.listener;


import com.fowo.api.flow.utils.SpringContextHolder;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @fileName: TaskCreatedListener
 * @Description: 代码目的，作用，如何工作
 * @Author: pzh
 * @Date: 2023/3/27 0027 10:42
 */
@Component
public class TaskCreatedListener implements TaskListener {
    @Resource
    private TaskService taskService;

    @Override
    public void notify(DelegateTask delegateTask) {
        String taskDefinitionKey = delegateTask.getTaskDefinitionKey();
        if ("root".equalsIgnoreCase(taskDefinitionKey)) {
            taskService.complete(delegateTask.getId());
        } else {
            if ("100000".equals(delegateTask.getAssignee())) {
                Object autoRefuse = delegateTask.getVariable("autoRefuse");
                if (autoRefuse == null) {
                    taskService.addComment(delegateTask.getId(), delegateTask.getProcessInstanceId(), "opinion", "审批人为空,自动通过");
                    taskService.complete(delegateTask.getId());
                } else {
                    taskService.addComment(delegateTask.getId(), delegateTask.getProcessInstanceId(), "opinion", "审批人为空,自动驳回");
                    RuntimeService runtimeService = SpringContextHolder.getBean(RuntimeService.class);
                    runtimeService.deleteProcessInstance(delegateTask.getProcessInstanceId(), "审批人为空,自动驳回");
                }
            }
        }
    }
}
